// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.FeeManage;
import com.fowo.api.model.fee.manage.FeeManageImportPo;
import com.fowo.api.model.fee.manage.FeeManageItemVo;
import com.fowo.api.model.fee.manage.FeeManageSearchParamPo;
import com.fowo.api.model.fee.manage.FeeManageVo;
import com.fowo.api.service.FeeManageService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "费用管理")
@RequestMapping("/api/fee-manage")
@RestController
public class FeeManageController {

  @Resource
  private FeeManageService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<FeeManageItemVo>> search(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<FeeManageItemVo>>> searchOptions(
    FeeManageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<FeeManageItemVo>> getPage(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    FeeManageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<FeeManageVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取费用单号相关的编号列表")
  @GetMapping("fee-code/{texts}/ids")
  public R<List<Long>> getIdsByFeeCode(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByFeeCode(nameList));
  }

  @Operation(summary = "获取编号相关的费用单号列表")
  @GetMapping("fee-code/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getFeeCodeByIds(idList));
  }

  @PreAuthorize("@am.canAccess('feeManageCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody FeeManageVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('feeManageEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody FeeManageVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('feeManageEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody FeeManageVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('feeManageDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('feeManageDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('feeManageImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<FeeManageImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('feeManageImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<FeeManageImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(response, "费用管理导入模版.xlsx");
    InputStream inputStream = new ClassPathResource("templates/FeeManage.xlsx")
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('feeManageExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    FeeManageSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('feeManageCancellation')")
  @Operation(summary = "作废")
  @PostMapping("cancellation")
  public R cancellation(@RequestBody IdPo po) {
    return R.run(() -> service.cancellation(po.getId()));
  }

  @PreAuthorize("@am.canAccess('feeManageAddModle')")
  @Operation(summary = "加入模板")
  @PostMapping("addModle")
  public R addModle(@RequestBody IdPo po) {
    return R.run(() -> service.addModle(po.getId()));
  }

  @PreAuthorize("@am.canAccess('feeManageAddFees')")
  @Operation(summary = "添加费用")
  @PostMapping("addFees")
  public R addFees() {
    return R.run(() -> service.addFees());
  }

  @PreAuthorize("@am.canAccess('feeManageImportFees')")
  @Operation(summary = "导入费用")
  @PostMapping("importFees")
  public R importFees(@RequestParam("file") MultipartFile file) {
    return R.run(() -> service.importFees(file));
  }

  @PreAuthorize("@am.canAccess('feeManageFeeTypeManage')")
  @Operation(summary = "费用类型管理")
  @PostMapping("feeTypeManage")
  public R feeTypeManage() {
    return R.run(() -> service.feeTypeManage());
  }

  @PreAuthorize("@am.canAccess('feeManageFeeModelManager')")
  @Operation(summary = "费用模板管理")
  @PostMapping("feeModelManager")
  public R feeModelManager() {
    return R.run(() -> service.feeModelManager());
  }

  /**
   * 视图选项卡标题上显示计数
   */
  @Operation(summary = "视图选项卡标题上显示计数")
  @GetMapping("getTabHeaderShowTotal")
  public R<Map> getTabHeaderShowTotal() {
    return R.data(() -> service.getTabHeaderShowTotal());
  }

  @Operation(summary = "全部")
  @GetMapping("all-fee")
  public R<List<FeeManageItemVo>> allFee(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allFee(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-feePage")
  public R<Page<FeeManageItemVo>> allFeePage(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allFeePage(search));
  }

  @Operation(summary = "全部包括子表的查询结果")
  @GetMapping("all-feePage-with-child")
  public R<Page<?>> allFeePageWithChild(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.allFeePageWithChildSearch(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-feeLastUpdateTime")
  public R<Date> allFeeLastUpdateTime(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allFeeLastUpdateTime(search));
  }

  @Operation(summary = "待提交")
  @GetMapping("wait-submit")
  public R<List<FeeManageItemVo>> waitSubmit(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitSubmit(search));
  }

  @Operation(summary = "待提交分页")
  @GetMapping("wait-submitPage")
  public R<Page<FeeManageItemVo>> waitSubmitPage(
    FeeManageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitSubmitPage(search));
  }

  @Operation(summary = "待提交包括子表的查询结果")
  @GetMapping("wait-submitPage-with-child")
  public R<Page<?>> waitSubmitPageWithChild(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.waitSubmitPageWithChildSearch(search));
  }

  @Operation(summary = "待提交最后更新时间")
  @GetMapping("wait-submitLastUpdateTime")
  public R<Date> waitSubmitLastUpdateTime(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitSubmitLastUpdateTime(search));
  }

  @Operation(summary = "已处理")
  @GetMapping("handled")
  public R<List<FeeManageItemVo>> handled(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.handled(search));
  }

  @Operation(summary = "已处理分页")
  @GetMapping("handledPage")
  public R<Page<FeeManageItemVo>> handledPage(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.handledPage(search));
  }

  @Operation(summary = "已处理包括子表的查询结果")
  @GetMapping("handledPage-with-child")
  public R<Page<?>> handledPageWithChild(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.handledPageWithChildSearch(search));
  }

  @Operation(summary = "已处理最后更新时间")
  @GetMapping("handledLastUpdateTime")
  public R<Date> handledLastUpdateTime(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.handledLastUpdateTime(search));
  }

  @Operation(summary = "已作废")
  @GetMapping("voided")
  public R<List<FeeManageItemVo>> voided(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.voided(search));
  }

  @Operation(summary = "已作废分页")
  @GetMapping("voidedPage")
  public R<Page<FeeManageItemVo>> voidedPage(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.voidedPage(search));
  }

  @Operation(summary = "已作废包括子表的查询结果")
  @GetMapping("voidedPage-with-child")
  public R<Page<?>> voidedPageWithChild(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.voidedPageWithChildSearch(search));
  }

  @Operation(summary = "已作废最后更新时间")
  @GetMapping("voidedLastUpdateTime")
  public R<Date> voidedLastUpdateTime(FeeManageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.voidedLastUpdateTime(search));
  }
}
